<template>
  <el-drawer title="学员编辑"
    v-model="drawer"
    size="800"
    @close="handleClose"
  >
    <el-form ref="refStudentForm"
      :model="store.student"
      :rules="rules"
      label-width="120px"
      style="width:90%"
      v-if="store.student"
    >
      <el-form-item label="头像" prop="avatar">
        <ImageUpload v-model="store.student.avatar" />
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input v-model="store.student.name" placeholder="请填写姓名" />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="store.student.phone" placeholder="请填写手机号" />
      </el-form-item>
      <el-form-item label="性别" prop="gender">
        <el-select v-model="store.student.gender" placeholder="请选择性别">
          <el-option label="男" :value="1" />
          <el-option label="女" :value="2" />
          <el-option label="未知" :value="0" />
        </el-select>
      </el-form-item>
      <el-form-item label="生日" prop="birthday">
        <el-date-picker v-model="store.student.birthday" type="date" placeholder="请选择生日" />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="store.student.email" placeholder="请填写邮箱" />
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input v-model="store.student.address" placeholder="请填写地址" />
      </el-form-item>
      <el-form-item label="爱好" prop="hobby">
        <el-input type="textarea" v-model="store.student.hobby" placeholder="请填写爱好" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSave">保存</el-button>
      </el-form-item>
    </el-form>
  </el-drawer>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useStudentStore } from '@/stores/student'
import { ElMessage } from 'element-plus'
import ImageUpload from '@/components/upload/single.vue'

const drawer = ref(false)
const refStudentForm = ref()
const store = useStudentStore()
const router = useRouter()
const route = useRoute()

const rules = {
  name: [{ required: true, message: '请填写姓名', trigger: 'blur' }],
  phone: [{required: true, message: '请填写手机', trigger: 'blur'}, 
  {validator: (rule: any, value: any, callback: any) => {
    if (!/^1[3-9]\d{9}$/.test(value)) {
      callback(new Error('手机号格式不正确'))
    } else {
      callback()
    }
  }, trigger: 'blur'}],
  gender: [{ required: true, message: '请选择性别', trigger: 'blur'}],
  email: [{ type: 'email', message: '请正确填写邮箱', trigger: 'blur'}]
}
const handleClose = () => {
  drawer.value = false
  router.back()
}
const handleSave = () => {
  refStudentForm.value.validate(async (valid : boolean) => {
    if (valid) {
      const result = await store.update()
      if (result) {
        ElMessage.success('修改成功')
        router.back()
        store.loadStudents()
      }
    }
  })
}

onMounted(() => {
  drawer.value = true
  if (route.params.id) {
    store.loadStudent(parseInt(route.params.id as string))
  } else {
    ElMessage.error('非法参数')
    drawer.value = false
  }
})
</script>